﻿using Newtonsoft.Json;
using SouthKoreaCoffee.Model;
using SouthKoreaCoffee.RobotModel;
using SouthKoreaCoffee.SocketClientsModel;
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace SouthKoreaCoffee.IOModel
{
    public class IOMsgShow
    {
        private static IOMsgShow instance;
        private IOMsgShow() { }

        public static IOMsgShow Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new IOMsgShow();
                }
                return instance;
            }
        }
        #region IO通讯
        public delegate void IOdelegate(string btnName, string tag);
        public IOdelegate iodelegate;
        public static bool IsMsgError = true;
        public bool isRobotBol = false;
        string nostrs = "";
        public void IOSerial_Thr()
        {
            try
            {
                var ddd = SerialPort.GetPortNames().ToList();
                if (!SerialPort.GetPortNames().ToList().Any(a => a.ToLower().Contains(IPorPortInfo.Instance.PortName)))
                {
                    Thread.Sleep(1000);
                    IOSerial_Thr();
                }
                IOSerialPortShow.Instance.IceMachineConnect(IPorPortInfo.Instance.PortName, 38400, "", 8, 1);//其他串口通讯
                IOSerialPortShow.Instance.serialPort.DataReceived += SerialPort_DataReceived;
                Thread All_thr = new Thread(AllMessages);
                All_thr.IsBackground = true;
                All_thr.Start();
            }
            catch (Exception)
            {
                IOSerialPort.Instance.serialPort.DataReceived -= SerialPort_DataReceived;
                Thread.Sleep(100);
                IOSerial_Thr();
            }
        }
        /// <summary>
        /// PING 通 IP
        /// </summary>
        /// <param name="IP"></param>
        /// <returns></returns>
        public bool PingIP(string IP)
        {
            try
            {
                if (IP.Split('.').Count() < 4) return false;
                Ping pingSender = new Ping();
                PingReply reply = pingSender.Send(IP, 120);//第一个参数为ip地址，第二个参数为ping的时间
                if (reply.Status == IPStatus.Success)
                {
                    return true;
                }
                else
                {
                    //ping不通
                    return false;
                }
            }
            catch (Exception)
            {
                return false;
            }
        }
        public void AllMessages()
        {
            try
            {
                while (true)
                {
                    try
                    {
                        IOSerialPortShow.Instance.SerialSend(DigitalMsg.Instance.AllMessage);
                        Thread.Sleep(100);
                    }
                    catch (Exception)
                    {
                        Thread.Sleep(100);
                    }

                }
            }
            catch (Exception)
            {
                IOSerialPort.Instance.serialPort.DataReceived -= SerialPort_DataReceived;
                Thread.Sleep(100);
                IOSerial_Thr();
            }
        }
        private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            try
            {
                var dd = IOSerialPortShow.Instance.serialPort.BytesToRead;
                byte[] result = new byte[dd];
                IOSerialPortShow.Instance.serialPort.Read(result, 0, dd);
                string str = "";// $"{DateTime.Now}：\n";
                str += $"{Encoding.ASCII.GetString(result)}";
                for (int i = 0; i < result.Length; i++)
                {
                    str += $"{result[i].ToString("X2")} ";
                }
                MsgShow(str);
                Thread.Sleep(1);
            }
            catch (Exception)
            {
                return;
            }
        }
        public void DigitalMsgBol2(string str)
        {
            try
            {
                var Bol = str.Split(' ');
                bool bol = false;
                if (WuLiaoCoffeeSel.Instance.YBJMilk_Sel_Bol != (Bol[4] == "01" ? true : false))
                {
                    bol = true;
                }
                WuLiaoCoffeeSel.Instance.YBJMilk_Sel_Bol = Bol[4] == "01" ? true : false;

                if (WuLiaoCoffeeSel.Instance.Milk_Sel_Bol != (Bol[6] == "01" ? true : false))
                {
                    bol = true;
                }
                WuLiaoCoffeeSel.Instance.Milk_Sel_Bol = Bol[6] == "01" ? true : false;
                if (WuLiaoCoffeeSel.Instance.YBJWater_Sel_Bol != (Bol[8] == "01" ? true : false))
                {
                    bol = true;
                }
                WuLiaoCoffeeSel.Instance.YBJWater_Sel_Bol = Bol[8] == "01" ? true : false;
                if (WuLiaoCoffeeSel.Instance.Water_Sel_Bol != (Bol[10] == "01" ? true : false))
                {
                    bol = true;
                }
                WuLiaoCoffeeSel.Instance.Water_Sel_Bol = Bol[10] == "01" ? true : false;
                if (WuLiaoCoffeeSel.Instance.TakeCup_Sel_Bol != (Bol[12] == "01" ? true : false))
                {
                    bol = true;
                }
                WuLiaoCoffeeSel.Instance.TakeCup_Sel_Bol = Bol[12] == "01" ? true : false;
                if (WuLiaoCoffeeSel.Instance.OutMealCup_Sel_Bol != (Bol[14] == "01" ? true : false))
                {
                    bol = true;
                }
                WuLiaoCoffeeSel.Instance.OutMealCup_Sel_Bol = Bol[14] == "01" ? true : false;
                if (WuLiaoCoffeeSel.Instance.LBQFK1_Bol != (Bol[16] == "01" ? true : false))
                {
                    bol = true;
                }
                WuLiaoCoffeeSel.Instance.LBQFK1_Bol = Bol[16] == "01" ? true : false;

                if (WuLiaoCoffeeSel.Instance.CupFeed1_Sel_Bol != (Bol[18] == "01" ? true : false))
                {
                    bol = true;
                }
                WuLiaoCoffeeSel.Instance.CupFeed1_Sel_Bol = Bol[18] == "01" ? true : false;
                if (WuLiaoCoffeeSel.Instance.LBQFK2_Bol != (Bol[20] == "01" ? true : false))
                {
                    bol = true;
                }
                WuLiaoCoffeeSel.Instance.LBQFK2_Bol = Bol[20] == "01" ? true : false;
                if (WuLiaoCoffeeSel.Instance.CupFeed2_Sel_Bol != (Bol[22] == "01" ? true : false))
                {
                    bol = true;
                }
                WuLiaoCoffeeSel.Instance.CupFeed2_Sel_Bol = Bol[22] == "01" ? true : false;
                if (WuLiaoCoffeeSel.Instance.BANNER1_Sel_Bol != (Bol[24] == "01" ? true : false))
                {
                    bol = true;
                }
                WuLiaoCoffeeSel.Instance.BANNER1_Sel_Bol = Bol[24] == "01" ? true : false;
                if (WuLiaoCoffeeSel.Instance.BANNER2_Sel_Bol != (Bol[26] == "01" ? true : false))
                {
                    bol = true;
                }
                WuLiaoCoffeeSel.Instance.BANNER2_Sel_Bol = Bol[26] == "01" ? true : false;
                if (WuLiaoCoffeeSel.Instance.PushRodUP1_Sel_Bol != (Bol[28] == "01" ? true : false))
                {
                    bol = true;
                }
                WuLiaoCoffeeSel.Instance.PushRodUP1_Sel_Bol = Bol[28] == "01" ? true : false;
                if (WuLiaoCoffeeSel.Instance.PushRodDown1_Sel_Bol != (Bol[30] == "01" ? true : false))
                {
                    bol = true;
                }
                WuLiaoCoffeeSel.Instance.PushRodDown1_Sel_Bol = Bol[30] == "01" ? true : false;
                if (WuLiaoCoffeeSel.Instance.OutMealCup2_Sel_Bol != (Bol[32] == "01" ? true : false))
                {
                    bol = true;
                }
                WuLiaoCoffeeSel.Instance.OutMealCup2_Sel_Bol = Bol[32] == "01" ? true : false;
                if (WuLiaoCoffeeSel.Instance.Levulose1_Sel_Bol != (Bol[34] == "01" ? true : false))
                {
                    bol = true;
                }
                WuLiaoCoffeeSel.Instance.Levulose1_Sel_Bol = Bol[34] == "01" ? true : false;
                if (WuLiaoCoffeeSel.Instance.Levulose2_Sel_Bol != (Bol[36] == "01" ? true : false))
                {
                    bol = true;
                }
                WuLiaoCoffeeSel.Instance.Levulose2_Sel_Bol = Bol[36] == "01" ? true : false;
                if (WuLiaoCoffeeSel.Instance.Levulose3_Sel_Bol != (Bol[38] == "01" ? true : false))
                {
                    bol = true;
                }
                WuLiaoCoffeeSel.Instance.Levulose3_Sel_Bol = Bol[38] == "01" ? true : false;
                if (WuLiaoCoffeeSel.Instance.YBJMilk2_Sel_Bol != (Bol[40] == "01" ? true : false))
                {
                    bol = true;
                }
                WuLiaoCoffeeSel.Instance.YBJMilk2_Sel_Bol = Bol[40] == "01" ? true : false;
                if (WuLiaoCoffeeSel.Instance.Milk2_Sel_Bol != (Bol[42] == "01" ? true : false))
                {
                    bol = true;
                }
                WuLiaoCoffeeSel.Instance.Milk2_Sel_Bol = Bol[42] == "01" ? true : false;
                if (WuLiaoCoffeeSel.Instance.YBJWater2_Sel_Bol != (Bol[44] == "01" ? true : false))
                {
                    bol = true;
                }
                WuLiaoCoffeeSel.Instance.YBJWater2_Sel_Bol = Bol[44] == "01" ? true : false;
                if (WuLiaoCoffeeSel.Instance.Water2_Sel_Bol != (Bol[46] == "01" ? true : false))
                {
                    bol = true;
                }
                WuLiaoCoffeeSel.Instance.Water2_Sel_Bol = Bol[46] == "01" ? true : false;
                if (WuLiaoCoffeeSel.Instance.PushRodUP2_Sel_Bol != (Bol[48] == "01" ? true : false))
                {
                    bol = true;
                }
                WuLiaoCoffeeSel.Instance.PushRodUP2_Sel_Bol = Bol[48] == "01" ? true : false;
                if (WuLiaoCoffeeSel.Instance.PushRodDown2_Sel_Bol != (Bol[50] == "01" ? true : false))
                {
                    bol = true;
                }
                WuLiaoCoffeeSel.Instance.PushRodDown2_Sel_Bol = Bol[50] == "01" ? true : false;
                if (WuLiaoCoffeeSel.Instance.YBJWater3_Sel_Bol != (Bol[52] == "01" ? true : false))
                {
                    bol = true;
                }
                WuLiaoCoffeeSel.Instance.YBJWater3_Sel_Bol = Bol[52] == "01" ? true : false;
                if (WuLiaoCoffeeSel.Instance.Water3_Sel_Bol != (Bol[54] == "01" ? true : false))
                {
                    bol = true;
                }
                WuLiaoCoffeeSel.Instance.Water3_Sel_Bol = Bol[54] == "01" ? true : false;
                if (WuLiaoCoffeeSel.Instance.YBJWater4_Sel_Bol != (Bol[56] == "01" ? true : false))
                {
                    bol = true;
                }
                WuLiaoCoffeeSel.Instance.YBJWater4_Sel_Bol = Bol[56] == "01" ? true : false;
                if (WuLiaoCoffeeSel.Instance.Water4_Sel_Bol != (Bol[58] == "01" ? true : false))
                {
                    bol = true;
                }
                WuLiaoCoffeeSel.Instance.Water4_Sel_Bol = Bol[58] == "01" ? true : false;
                if (WuLiaoCoffeeSel.Instance.Weigh1_Sel_Bol != (Bol[60] == "01" ? true : false))
                {
                    bol = true;
                }
                WuLiaoCoffeeSel.Instance.Weigh1_Sel_Bol = Bol[60] == "01" ? true : false;
                if (WuLiaoCoffeeSel.Instance.DiscardCup1_Sel_Bol != (Bol[62] == "01" ? true : false))
                {
                    bol = true;
                }
                WuLiaoCoffeeSel.Instance.DiscardCup1_Sel_Bol = Bol[62] == "01" ? true : false;
                if (WuLiaoCoffeeSel.Instance.DiscardCup2_Sel_Bol != (Bol[64] == "01" ? true : false))
                {
                    bol = true;
                }
                WuLiaoCoffeeSel.Instance.DiscardCup2_Sel_Bol = Bol[64] == "01" ? true : false;
                if (WuLiaoCoffeeSel.Instance.DiscardCup3_Sel_Bol != (Bol[66] == "01" ? true : false))
                {
                    bol = true;
                }
                WuLiaoCoffeeSel.Instance.DiscardCup3_Sel_Bol = Bol[66] == "01" ? true : false;
                if (bol)
                {
                    ClientList clientList = new ClientList();
                    clientList.MsgBol = WuLiaoCoffeeSel.Instance;
                    clientList.code = 2;
                    SocketClients.Instance.client.Send(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(clientList)));
                }
            }
            catch (Exception)
            {
                return;
            }
        }
        string strlist = "";
        public void MsgShow(string str)
        {
            try
            {
                // Console.WriteLine(str);
                nostrs = "";
                var nostr = str.Split(' ');
                if (nostr.Length >= 2)
                    nostrs += nostr[0].Substring(nostr[0].Length - 2, 2) + " ";
                for (int i = 1; i < nostr.Count() - 1; i++)
                {
                    nostrs += nostr[i] + " ";
                }
                if (nostrs.Length >= 5)
                {
                    if (nostrs.Substring(0, 5) == "01 05")
                    {
                        Console.WriteLine(str);
                    }
                    else if (nostrs.Substring(0, 5) == "01 04")
                    {
                        strlist = nostrs;
                    }
                    else
                    {
                        if (strlist != "")
                            strlist += nostrs;
                    }
                }
                if (strlist.Split(' ').Count() >= 105)
                {
                    DigitalMsgBol2(strlist);
                    strlist = "";
                }
            }
            catch (Exception ex)
            {
                return;
            }
        }
        #endregion
    }
}
